﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;

public partial class Controls_SettingView : Pages_Base
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSettingGroups();
            if (Request["g_id"] != null)
                ddlSettingGroups.SelectedValue = Request["g_id"].ToString();
            loadData();
        }
    }
    private void loadData()
    {
        try
        {
            objDT = BO_Settings.SelectByGroupId(Convert.ToInt32(ddlSettingGroups.SelectedValue), 1);
            bindRepeater(rptData, objDT, divMessage);
        }
        catch (Exception ex) { throw ex; }
        finally { }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
    }
    protected void loadSettingGroups()
    {
        try
        {
            //DataTable objDT = null;//BO_Categories.SelectParent(c_id, 1);
            objDT = BO_SettingGroups.SelectAll(1);
            ddlSettingGroups.DataTextField = "FULL_SETTING_NAME";
            ddlSettingGroups.DataValueField = "SETTING_GROUP_ID";
            ddlSettingGroups.DataSource = objDT;
            ddlSettingGroups.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { }
    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            Button btnID = (Button)e.Item.FindControl("btnID");
            int s_id = Convert.ToInt32(btnID.CommandArgument);
            string commandName = e.CommandName;
            if (commandName.Equals("del"))
            {
                BO_Settings.Delete(s_id);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
            }
            //else if (commandName.Equals("edit"))
            //    Response.Redirect("default.aspx?module=settingedit&s_id=" + s_id);
            //else if (commandName.Equals("Up"))
            //    BO_se.Up(c_id);
            //else if (commandName.Equals("Down"))
            //    BO_Categories.Down(c_id);
            loadData();
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
        finally { }
    }
    protected void btnViewTrash_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx?module=ViewNewsTrash");
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        DataTable admin = (DataTable)Session["Admin"];
        Button btnID;
        foreach (RepeaterItem item in rptData.Items)
        {
            btnID = (Button)item.FindControl("btnID");
            if (btnID == null)
                return;
            objDT = BO_Settings.Search(Convert.ToInt32(btnID.CommandArgument));
            string value = String.Empty;
            switch (Convert.ToInt32(objDT.Rows[0]["SETTING_TYPE"].ToString()))
            {
                case 0:
                    TextBox textarea = (TextBox)item.FindControl("textarea");
                    value = textarea.Text.Trim();
                    break;
                case 1:
                    TextBox txtValue = (TextBox)item.FindControl("txtValue");
                    value = txtValue.Text.Trim();
                    break;
                case 2:
                    RadioButtonList rbl = (RadioButtonList)item.FindControl("rbl");
                    value = rbl.SelectedItem.Text;
                    break;
                case 3:
                    DropDownList ddl = (DropDownList)item.FindControl("ddl");
                    value = ddl.SelectedItem.Text;
                    break;
                case 4:
                    CheckBox cbo = (CheckBox)item.FindControl("cbo");
                    value = cbo.Checked.ToString();
                    break;
            }
            string result = BO_Settings.Update(Convert.ToInt32(objDT.Rows[0]["SETTING_ID"]), Convert.ToInt32(objDT.Rows[0]["SETTING_GROUP_ID"]), objDT.Rows[0]["SETTING_KEY"].ToString(), objDT.Rows[0]["SETTING_NAME"].ToString(), objDT.Rows[0]["SETTING_DESCRIPTION"].ToString(), value, Convert.ToInt32(objDT.Rows[0]["SETTING_TYPE"].ToString()), objDT.Rows[0]["SETTING_TYPE_VALUE"].ToString(), Convert.ToInt32(objDT.Rows[0]["SORT_DISPLAY"]), Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"].ToString()), objDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), true);
            if (!result.Equals("0"))
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Cập nhật thành công .");
            else
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được !");
        }
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Button btnID = (Button)e.Item.FindControl("btnID");
        if (btnID == null)
            return;
        objDT = BO_Settings.Search(Convert.ToInt32(btnID.CommandArgument));
        if (objDT != null && objDT.Rows.Count > 0)
        {
            ListItem item;
            string strValue = objDT.Rows[0]["SETTING_VALUE"].ToString().Trim();
            string strTypeValue = objDT.Rows[0]["SETTING_TYPE_VALUE"].ToString().Trim();
            string[] lstTypeValue = strTypeValue.Split(new string[] { "/-/" }, StringSplitOptions.RemoveEmptyEntries);
            switch (Convert.ToInt32(objDT.Rows[0]["SETTING_TYPE"].ToString()))
            {
                case 0:
                    TextBox textarea = e.Item.FindControl("textarea") as TextBox;
                    textarea.Text = strValue;
                    textarea.Width = new Unit(100, UnitType.Percentage);
                    textarea.Visible = true;
                    break;
                case 1:
                    TextBox txtValue = e.Item.FindControl("txtValue") as TextBox;
                    txtValue.Text = strValue;
                    txtValue.Width = new Unit(100, UnitType.Percentage);
                    txtValue.Visible = true;
                    break;
                case 2:
                    RadioButtonList rbl = e.Item.FindControl("rbl") as RadioButtonList;
                    rbl.Width = new Unit(100, UnitType.Percentage);
                    foreach (string per in lstTypeValue)
                    {
                        ListItem rdoValue = new ListItem(per);
                        rbl.Items.Add(rdoValue);
                    }
                    if (!strValue.Equals(String.Empty))
                    {
                        item = rbl.Items.FindByText(strValue);
                        if (item != null)
                            item.Selected = true;
                    }
                    rbl.Visible = true;
                    break;
                case 3:
                    DropDownList ddl = (DropDownList)e.Item.FindControl("ddl");
                    ddl.Width = new Unit(100, UnitType.Percentage);
                    foreach (string per in lstTypeValue)
                    {
                        ListItem ddlItem = new ListItem(per);
                        ddl.Items.Add(ddlItem);
                    }
                    if (!strValue.Equals(String.Empty))
                    {
                        item = ddl.Items.FindByText(strValue);
                        if (item != null)
                            item.Selected = true;
                    }
                    ddl.Visible = true;
                    break;
                case 4:
                    CheckBox cbo = (CheckBox)e.Item.FindControl("cbo");
                    cbo.Visible = true;
                    cbo.Checked = Convert.ToBoolean(strValue);
                    break;
            }
        }
    }
    protected void ddlSettingGroups_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = base.removeRequest();
        if (Request["g_id"] != null)
        {
            int conti = url.LastIndexOf("g_id=");
            string start = url.Substring(0, url.LastIndexOf("&g_id="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        Response.Redirect(url + "&g_id=" + ddlSettingGroups.SelectedValue);
    }
}